package com.wzx.ptplatform.controller.admin.student;

import com.wzx.ptplatform.domain.WorkExperience;
import com.wzx.ptplatform.service.WorkExperienceService;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员学生模块——简历信息工作经历管理")
@RestController
public class AdminStudentWorkExperienceController {

    final WorkExperienceService workExperienceService;

    public AdminStudentWorkExperienceController(WorkExperienceService workExperienceService) {
        this.workExperienceService = workExperienceService;
    }

    @RequestMapping("admin_student_work_experience_message_list")
    public LayuiPager<WorkExperience> adminStudentWorkExperienceMessageList(
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        WorkExperience workExperience = new WorkExperience();
        Page<WorkExperience> pager = workExperienceService.findList(workExperience, page - 1, limit);
        LayuiPager<WorkExperience> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_student_work_experience_message_editView")
    public ModelAndView adminStudentWorkExperienceMessageEditView(String id, Model model) {
        model.addAttribute("workExperience", workExperienceService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/student/work_experience_message_edit");
    }

    @PostMapping("admin_student_work_experience_message_save_or_edit")
    public Map<String, Object> adminStudentWorkExperienceMessageSaveOrEdit(WorkExperience workExperience) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (workExperienceService.saveOrEdit(workExperience)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_student_work_experience_message_delete")
    public Map<String, Object> adminStudentWorkExperienceMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (workExperienceService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }
}
